package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.LoadArticleBehaviorService;
import com.heima.common.exception.CustException;
import com.heima.common.user.UserRelationConstants;
import com.heima.model.behavior.dtos.ArticleBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApCollection;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * @author huan
 * @serial 每天一百行, 致敬未来的自己
 * @Description
 */
@Service
@Slf4j
public class LoadArticleBehaviorServiceImpl implements LoadArticleBehaviorService {
    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Override
    public ResponseResult loadArticleBehavior(ArticleBehaviorDTO dto) {
        //用户是否登录
        ApUser apUser = AppThreadLocalUtils.getUser();
        HashMap<String, Object> map = new HashMap();
        if (apUser == null) {
            map.put("isfollow", false);
            map.put("islike", false);
            map.put("isunlike", false);
            map.put("iscollection", false);
            return ResponseResult.okResult(map);
        } else {
            //查询行为实体
            ApBehaviorEntry entry = apBehaviorEntryService.findByUserIdOrEquipmentId(apUser.getId(), dto.getEquipmentId());
            if (entry == null) {
                //entry == null 代表 UserId 与 EquipmentId 同时为null
                CustException.cust(AppHttpCodeEnum.NEED_LOGIN, "请使用账户或设备进行登录");
            }
            //获取行为查询条件
            Query query = Query.query(Criteria.where("entryId").is(entry.getId())
                    .and("articleId").is(dto.getArticleId()));
            map.put("iscollection", mongoTemplate.exists(query, ApCollection.class));
            map.put("islike", mongoTemplate.exists(query, ApLikesBehavior.class));
            map.put("isunlike", mongoTemplate.exists(query, ApUnlikesBehavior.class));
            //根据登录用户id 去redis中查询是否关注该作者
            Double score = stringRedisTemplate.opsForZSet().score(UserRelationConstants.FOLLOW_LIST + apUser.getId(), String.valueOf(dto.getAuthorApUserId()));
            if (score == null) {
                //没有关注
                map.put("isfollow", false);
            } else {
                map.put("isfollow", true);
            }
        }
        return ResponseResult.okResult(map);
    }
}
